package cn.zhage.aimin.search1.controller;

import cn.zhage.aimin.base.model.DrugIndex;
import cn.zhage.aimin.search1.mapper.DrugIndexMapper;
import lombok.RequiredArgsConstructor;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/*
 *
 * @author 渣哥
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {

    private final DrugIndexMapper drugIndexMapper;

/*    private final ElasticsearchTemplate elasticsearchTemplate;

    @RequestMapping("/create")
    public String hello() {
        IndexOperations indexOperations = elasticsearchTemplate.indexOps(DrugIndex.class);
        if(!indexOperations.exists()){
            indexOperations.create();
            indexOperations.putMapping(DrugIndex.class);
        }
        return "hello";
    }*/
    @GetMapping("/search")
    public List<DrugIndex> search(String keyword) {
        // 3.查询出所有标题为老汉的文档列表
        LambdaEsQueryWrapper<DrugIndex> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(DrugIndex::getName, keyword)
                .or()
                .eq(DrugIndex::getGenericName, keyword);
        return drugIndexMapper.selectList(wrapper);
    }

    @GetMapping("/delete")
    public void delete(String id) {
        drugIndexMapper.deleteById(id);
    }

    @GetMapping("/insert")
    public Integer insert() {
        // 2.初始化-> 新增数据
        DrugIndex drug2 = new DrugIndex();
        drug2.setDrugId(10044);
        drug2.setName("复方板蓝根颗粒");
        drug2.setGenericName("板蓝根");
        drug2.setCategoryId(8);
        drug2.setCategoryName("中成药");
        drug2.setBrand("白云山");
        drug2.setManufacturer("广州白云山和记黄埔中药有限公司");
        drug2.setDescription("具有清热解毒，凉血利咽的功效。用于风热感冒、咽喉肿痛、口咽干燥。");
        drug2.setDosageForm("颗粒剂");
        drug2.setStrength("10g*20袋");
        drug2.setPrice(new BigDecimal("32.00"));
        drug2.setDiscountPrice(null);  // 无折扣
        drug2.setPrescription(false);
        drug2.setShelfLife("24个月");
        drug2.setBarCode("6924567890123");
        drug2.setStorageInstructions("遮光、密封保存");
        drug2.setCreateAt(new Date());
        drug2.setUpdateAt(new Date());
        drug2.setStatus(1);
        drug2.setImgUrl("https://example.com/images/banlangen.jpg");
        return drugIndexMapper.insert(drug2);
    }

    @GetMapping("/like")
    public List<DrugIndex> like(String keyword) {
        LambdaEsQueryWrapper<DrugIndex> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.match(DrugIndex::getName, keyword);
        return drugIndexMapper.selectList(wrapper);
    }

}
